package cn.rdq.iknessplatform.service.impl;

import cn.rdq.api.po.Drama;
import cn.rdq.api.vo.DramaVo;
import cn.rdq.dao.DramaMapper;
import cn.rdq.iknessplatform.service.DramaService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DramaServiceImpl extends ServiceImpl<DramaMapper, Drama> implements DramaService {

    @Autowired
    private DramaMapper dramaMapper;

    @Override
    public List<Drama> query(DramaVo dramaVo) {
        QueryWrapper<Drama> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dramaVo.getStatus())) {
            wrapper.eq("status", dramaVo.getStatus());
        }
        if (!StringUtils.isEmpty(dramaVo.getCategory())) {
            wrapper.eq("category", dramaVo.getCategory());
        }
        if (!StringUtils.isEmpty(dramaVo.getArea())) {
            wrapper.eq("area", dramaVo.getArea());
        }
        if (StringUtils.isNotEmpty(dramaVo.getTitle())) {
            wrapper.like("title", dramaVo.getTitle());
        }
        wrapper.orderByDesc("create_time");
        PageHelper.startPage(dramaVo.getCurrentPage(), dramaVo.getPageSize());
        return list(wrapper);
    }

    @Override
    public List<Drama> lastByCategory(String category) {
        QueryWrapper<Drama> wrapper = new QueryWrapper<>();
        wrapper.eq("category", category)
                .orderByDesc("publish_time");
        PageHelper.startPage(1, 9);
        return list(wrapper);
    }

    @Override
    public List<Drama> top9ByPublishTimeByArea(String area) {
        QueryWrapper<Drama> wrapper = new QueryWrapper<>();
        wrapper.eq("area", area)
                .eq("category", 2)
                .orderByDesc("publish_time");
        PageHelper.startPage(1, 9);
        return list(wrapper);
    }

    @Override
    public List<Drama> mostViews() {
        QueryWrapper<Drama> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("views");
        PageHelper.startPage(1, 10);
        return list(wrapper);
    }
}
